﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraMovementController : MonoBehaviour {
	public float Sensitivity;

	private Vector3 m_Movement;

	private Vector3 m_PrePosition;

	private float m_Width;
    private float m_Height;
	private Vector2 m_WidthRange;
	private Vector2 m_HeightRange;

	private void Awake() {
		SetBasicValues();

		m_WidthRange = new Vector2(
			-GameController.Instance.SceneWidth / 2 + m_Width / 2,
			GameController.Instance.SceneWidth / 2 - m_Width / 2
		);

		m_HeightRange = new Vector2(
			-GameController.Instance.SceneHeight / 2 + m_Height / 2,
			GameController.Instance.SceneHeight / 2 - m_Height / 2
		);
	}

	private void Update() {
		if(Input.GetMouseButton(1)) {
			float x = Input.GetAxis("Mouse X");
			float y = Input.GetAxis("Mouse Y");
			m_Movement = new Vector3(x, y, 0f);

			this.transform.Translate(-m_Movement * Sensitivity * Time.deltaTime, Space.World);
			this.transform.position = new Vector3 (
				Mathf.Clamp(this.transform.position.x, m_WidthRange.x, m_WidthRange.y),
				Mathf.Clamp(this.transform.position.y, m_HeightRange.x, m_HeightRange.y),
				this.transform.position.z
			);

			m_PrePosition = Input.mousePosition;
		}
	}

	// 获取摄像机的宽度和高度
    private void SetBasicValues(){
        float leftBorder;
        float rightBorder;
        float topBorder;
        float downBorder;
        //the up right corner
        Vector3 cornerPos=Camera.main.ViewportToWorldPoint(new Vector3(1f,1f,Mathf.Abs(Camera.main.transform.position.z)));

        leftBorder=Camera.main.transform.position.x-(cornerPos.x-Camera.main.transform.position.x);
        rightBorder=cornerPos.x;
        topBorder=cornerPos.y;
        downBorder=Camera.main.transform.position.y-(cornerPos.y-Camera.main.transform.position.y);

        m_Width=rightBorder-leftBorder;
        m_Height=topBorder-downBorder;
    }
}
